// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PROD_WIP_HIS
// * CreateDate    : 2014-02-18 18:04:07
// * Creater       : CodeSmith
// * LastChangeDate: 2014-02-18 18:04:07
// * LastChanger   : 2014-02-18 18:04:07
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Prod
{
/// <summary>
    /// ProdWipHis
    /// </summary>
    public partial class ProdWipHisBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objProdWipHisEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ProdWipHisEntity GetProdWipHis(ProdWipHisEntity objProdWipHisEntity)
        {
            return DataAccess.SelectSingle<ProdWipHisEntity>(objProdWipHisEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objProdWipHisEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertProdWipHis(ProdWipHisEntity objProdWipHisEntity)
        {
            return DataAccess.Insert(objProdWipHisEntity);
        }

        public static bool InsertProdWipHis(List<ProdWipHisEntity> objProdWipHisEntityList)
        {
            return DataAccess.Insert(objProdWipHisEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objProdWipHisEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateProdWipHis(ProdWipHisEntity objProdWipHisEntity)
        {
            return DataAccess.Update(objProdWipHisEntity);
        }

        public static bool UpdateProdWipHis(List<ProdWipHisEntity> objProdWipHisEntityList)
        {
            return DataAccess.Update<ProdWipHisEntity>(objProdWipHisEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objProdWipHisEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteProdWipHis(ProdWipHisEntity objProdWipHisEntity)
        {
            return DataAccess.Delete(objProdWipHisEntity);
        }

        public static bool DeleteProdWipHis(List<ProdWipHisEntity> objProdWipHisEntityList)
        {
            return DataAccess.Delete<ProdWipHisEntity>(objProdWipHisEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetProdWipHisAll()
        {
            return DataAccess.SelectAllDataSet<ProdWipHisEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ProdWipHisEntity> GetProdWipHisAllList()
        {
            return DataAccess.SelectAll<ProdWipHisEntity>();
        }
        #endregion


        public static DataSet GetWipHisProcs(ProdWoTaskCondition condition)
        {
            string sqlCondition = "";
            if (condition != null)
            {
                if (!string.IsNullOrEmpty(condition.WoNo))
                    sqlCondition += " And wo.Wo_No like '%" + condition.WoNo + "%'";
                if (!string.IsNullOrEmpty(condition.ProductId))
                    sqlCondition += " And wo.Product_Code = '" + condition.ProductId + "'";
                if (!string.IsNullOrEmpty(condition.UserNo))
                    sqlCondition += " And his.User_No like '%" + condition.UserNo + "%'";
            }

            string sql = @"select distinct  his.Cur_Technic_No,his.Cur_Technic_Name 
            from dbo.PROD_WO wo left join dbo.PROD_WIP_HIS his
            on wo.WO_NO=his.WO_No
            where wo.STATUS in('{0}','{1}') and  his.Cur_Technic_No is not null " + sqlCondition;
            sql = string.Format(sql, Common.WoStatus.WOS_Active.ToString(), Common.WoStatus.WOS_Run.ToString());
            return DataAccess.SelectDataSet(sql); 
            
        }


        public static DataSet GetWipKPI(ProdWoTaskCondition condition)
        {
            string sqlCondition = "";
            if (condition != null)
            {
                if (!string.IsNullOrEmpty(condition.WoNo))
                    sqlCondition += " And wo.Wo_No like '%" + condition.WoNo + "%'";
                if (!string.IsNullOrEmpty(condition.ProductId))
                    sqlCondition += " And wo.Product_Code = '" + condition.ProductId + "'";
                if (!string.IsNullOrEmpty(condition.UserNo))
                    sqlCondition += " And his.User_No like '%" + condition.UserNo + "%'";
            }

            string sql = @"select SUM(his.Prod_Qty) QTY, his.WO_NO,his.Cur_Technic_No,his.Cur_Technic_Name ,his.User_No
            from dbo.PROD_WO wo left join dbo.PROD_WIP_HIS his
            on wo.WO_NO=his.WO_No
            where  his.Event_Type='logoff' {0} 
            group by his.WO_NO,his.Cur_Technic_No,his.Cur_Technic_Name ,his.User_No,his.Technic_Seq
            order by WO_NO,User_No,his.Technic_Seq" ;
            sql = string.Format(sql, sqlCondition);
            return DataAccess.SelectDataSet(sql); 

        }


        public static DataSet GetWorkDaily(DateTime date)
        {
            string sql = @"select SUM(his.Prod_Qty) QTY, his.WO_NO,his.Cur_WorkGroup_No WorkGroup_No, his.Cur_WorkGroup_Name WorkGroup_Name
            from  dbo.PROD_WIP_HIS his
            where  his.Event_Type='logoff' and Convert(date,his.Event_Time)=CONVERT(date,'{0}')
            group by  his.WO_NO,his.Cur_WorkGroup_No, his.Cur_WorkGroup_Name
            order by his.WO_NO";
            sql = string.Format(sql, date.ToString());
            return DataAccess.SelectDataSet(sql); 
        }


	}
	
	
}	

